﻿window.xcookiekeys = function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = unescape(aKeys[nIdx]); }
    return aKeys;
};

window.xcookievalues = function () {
    var result = new Array();
    var keys = window.xcookiekeys();
    for (var i = 0; i < keys.length; i++) { result.push(document.cookies.getValue(keys[i])); }
    return result;
}

window.xcookieenabled = function () {
    if (navigator.cookieEnabled) return true;
    var testKey = "cookietest-jscookit";
    document.cookies.save(new DocumentCookie(testKey, "itworks"));
    var result = document.cookies.hasKey(testKey) && document.cookies.getValue(testKey) === "itworks";
    document.cookies.remove("cookietest-jscookit");
    return result;
}

var DocumentCookie = function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    this.Key = sKey;
    this.Value = sValue;
    this.Expiration = vEnd;
    this.Path = sPath;
    this.Domain = sDomain;
    this.IsSecure = bSecure;
};

document.cookies = {
    Keys: window.xcookiekeys(),
    Count: window.xcookiekeys().length,
    Values: window.xcookievalues(),
    Enabled: window.xcookieenabled()
};

document.cookies.compareValue = function (sKey, sValue) {
    if (document.cookies.hasKey(sKey) && document.cookies.getValue(sKey) == sValue) {
        return true;
    }
    return false;
}

document.cookies.clear = function () {
    var i;
    for (i = 0; i < document.cookies.Keys.length; i++) {
        document.cookies.removeCookie(document.cookies.Keys[i]);
    }
};

document.cookies.findKeys = function (sValue) {
    var collection = document.cookies.getKeyAndValues();
    var result = new Array();
    for (var i = 0; i < collection.length; i++) {
        if (collection[i].Value === sValue) result.push(collection[i].Key);
    }
    return result;
};

document.cookies.getKeyAndValues = function () {
    var result = new Array();
    for (var i = 0; i < document.cookies.Keys.length; i++) {
        var key = document.cookies.Keys[i];
        result.push(new DocumentCookie(key, document.cookies.getValue(key)));
    }
    return result;
};

document.cookies.getValue = function (sKey) {
    return unescape(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
};

document.cookies.hasKey = function (sKey) {
    return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
};

document.cookies.remove = function (sKey) {
    if (!sKey || !document.cookies.hasKey(sKey)) { return false; }
    document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
    return true;
};

document.cookies.save = function (objCookie) {
    if (!objCookie.Key || /^(?:expires|max\-age|path|domain|secure)$/i.test(objCookie.Key)) { return false; }
    var sExpires = "";
    if (objCookie.Expiration) {
        switch (objCookie.Expiration.constructor) {
            case Number:
                sExpires = objCookie.Expiration === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + objCookie.Expiration;
                break;
            case String:
                sExpires = "; expires=" + objCookie.Expiration;
                break;
            case Date:
                sExpires = "; expires=" + objCookie.Expiration.toGMTString();
                break;
        }
    }
    document.cookie = escape(objCookie.Key) + "=" + escape(objCookie.Value) + sExpires + (objCookie.Domain ? "; domain=" + objCookie.Domain : "") + (objCookie.Path ? "; path=" + objCookie.Path : "") + (objCookie.IsSecure ? "; secure" : "");
    return true;
};